package com.bookworld.util;

import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;

@Component
public class StringToBigDecimalConverter implements Converter<String, BigDecimal> {
    
    @Override
    public BigDecimal convert(String source) {
        if (source == null || source.trim().isEmpty()) {
            return null;
        }
        try {
            // 移除货币符号和千位分隔符
            String cleanString = source.trim()
                    .replaceAll("[^\\d.,-]", "")
                    .replace(",", "");
            return new BigDecimal(cleanString);
        } catch (NumberFormatException e) {
            throw new IllegalArgumentException("Invalid number format: " + source);
        }
    }
}
